// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SerializerTest.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Test.Robotics.IO
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Threading;    

    using Microsoft.Robotics.IO;
    using Microsoft.Robotics.Runtime;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Validates that message synchronization works as expected.
    /// </summary>
    [TestClass]
    public class SerializerTest
    {
        /// <summary>
        /// Validates the Serializer
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void SerializerIntTest()
        {
            int testValue = 5;
            const string Filename = "SerializerTestFile";

            try
            {
                Serializer.Save<int>(Filename, testValue);
                int loadValue = Serializer.Load<int>(Filename);

                Assert.AreEqual(testValue, testValue);
            }
            finally
            {
                // make sure we nuke the file if test fails
                File.Delete(Filename);
            }
        }

        /// <summary>
        /// Validates the Serializer for compressed arrays .
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void SerializerIntArrayCompressedTest()
        {
            TestSerializerIntArray(true);
        }

        /// <summary>
        /// Validates the Serializer for uncompressed arrays.
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void SerializerIntArrayUncompressedTest()
        {
            TestSerializerIntArray(false);
        }

        /// <summary>
        /// Validates manifest serialization
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void ManifestSerializationTest()
        {
            const string Filename = "ManifestSerializationTest.xml";

            try
            {
                Manifest manifest = new Manifest();
                Agent timer = new TimerAgent("Timer", 15);
                manifest.Agents.Add(timer);
                IEnumerable<Type> types = manifest.Agents.Select(a => a.GetType());

                Serializer.Save(Filename, manifest, types);

                // now read it back
                manifest = Serializer.Load<Manifest>(Filename);

                if (manifest.Agents.Count != 1)
                {
                    throw new Exception("Deserialization didn't work.");
                }
            }
            finally 
            {
                File.Delete(Filename);
            }
        }

        /// <summary>
        /// Tests if ThereIsNoDataLossWhenSerializingDoubleArrays
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void ThereIsNoDataLossWhenSerializingDoubleArrays()
        {
            const string Filename = "SerializerTestFile";

            double[][] arrayToSerialize = new double[3][]
                {
                    new double[] { 240, 241, 242, 243 },
                    new double[] { 300001, 300002, 300004, 30000f },
                    new double[] { 1351, 1353, 1355, 13570 }
                };  

            try
            {
                Serializer.Save<double[][]>(Filename, arrayToSerialize);
            
                double[][] testArray = Serializer.Load<double[][]>(Filename);

                Assert.IsNotNull(testArray);

                int width = testArray.Length;
                int height = testArray[0].Length;

                for (int i = 0; i < width; ++i) 
                {
                    for (int j = 0; j < height; ++j)
                    {
                        Assert.AreEqual(arrayToSerialize[i][j], testArray[i][j]);
                    }
                }
            }
            finally
            {
                File.Delete(Filename);
            }
        }

        /// <summary>
        /// Tests the serialization of arrays for the .
        /// </summary>
        /// <param name="useSerializableArrays">Specifies whether to user serializable arrays during serialization</param>
        private static void TestSerializerIntArray(bool useSerializableArrays)
        {
            int[] testValue = { 5, 7, 18 };
            const string Filename = "SerializerTestFile";

            try
            {
                Serializer.Save<int[]>(Filename, testValue, useSerializableArrays);
                int[] loadValue = Serializer.Load<int[]>(Filename, useSerializableArrays);
                for (int i = 0; i < testValue.Length; i++)
                {
                    Assert.AreEqual(testValue[i], loadValue[i]);
                }
            }
            finally
            {
                // make sure we nuke the file if test fails
                File.Delete(Filename);
            }
        }
    }
}
